<?php Util::registerScriptFile(Util::themeUrl().'/js/comment.js');?>
<?php if ($checkAsign !== false) :?>
<?php $form=$this->beginWidget('CActiveForm', array(
		'id'=>'confirm-asign',
		'enableAjaxValidation'=>false,
	)); ?>
<div id="action-post" <?php echo ($checkAsign === true) ? 'class = "notset"' : ''?>>
	<?php if ($checkAsign !== '2') : ?>
		<?php echo CHtml::submitButton('Tôi sẽ tham dự', array('name' => 'accept','class' => 'btn btn-default btn-accept')); ?>
	<?php endif;?>
	<?php if ($checkAsign !== '1') :?>
		<?php echo CHtml::textArea('unaccept', '', array('style' => 'display:none;')); ?>
		<?php echo CHtml::button('Không thể tham dự', array('class' => 'btn btn-default btn-igorn', 'id' => 'btn-igorn')); ?>
	<?php endif;?>
</div>
<?php $this->endWidget(); ?>
<!-- ---------------popup------------------- -->
<?php echo $this->renderPartial('_confirm_accept'); ?>
<?php endif;?>
<div class="listp">
	<h1><a class = "title-post"><?php echo $data->title; ?></a></h1>
	<?php $this->widget('application.components.InfoPost', compact('data')); ?>
	<div class="post-preview-text">
		<?php echo $data->content; ?>
	</div>
	<hr class="hrpadding" id = "target-comment-reload">
</div>
<?php $this->widget('application.components.CommentPost', array('id' => $data->id)); ?>


<script type="text/javascript">
$(document).ready(function() {
	$('.submit-comment, .submit-reply').live("click", function(){
		var parent_id	= '#comment-post';
		var isReply		= false;
		if (this.id != 'submit-comment-id'){
			isReply		= true;
			parent_id	= '#' + $(this).closest('.post-reply-comment').attr('id');
			var parentComment = parent_id.replace('#comment-reply-', '');
		}

		// Validate
		if(!validateFormComment(parent_id))
			return false;

		// Set param post
		var params 	= 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
        params += '&content=' + $(parent_id + ' #comment-post-content').val();
        params += '&post_id=' + <?php echo $data->id;?>;
       	params += (isReply) ? ('&parent_id=' + parentComment) : '';

		<?php if (Yii::app()->user->isGuest) :?>
        	params += '&name=' + $(parent_id + ' #comment-name').val();
            params += '&email=' + $(parent_id + ' #comment-email').val();
        <?php else :?>
        	params += '&user_id=' + <?php echo Yii::app()->user->id?>;
        <?php endif;?>

        // Execute ajax
        $.ajax({
                type: "POST",
                url: "<?php echo Yii::app()->createUrl('site/submitComment') ?>",
                data: params,
                success: function(data) {
        			// Show loading process
        			$(parent_id + ' #ajax-loading').show();
        			// Disable button
        			$(parent_id + ' .form-actions input[type="button"]').hide();

					// Process
					$(parent_id + ' #ajax-loading' ).find('div').animate({width: '170%'}, 600, function() {
                        if (isReply) {
                        	$("#comment-reply-" + parentComment).before(data);
                        	$("#comment-reply-" + parentComment).prev('div.replycomment').effect("highlight", { color: "#ffc68c" }, 1200);
                        	clearFormReply();
                        }
                        else {
							// Hide process
							$( "#ajax-loading" ).hide();
							$( "#ajax-loading" ).find('div').css('width', '30%');

							// Focus to position new comment show
                     		offset = $("#target-comment-reload").offset();
                 			$("html,body").animate({
                 			    scrollTop: offset.top,
                 			    scrollLeft: offset.left
                 			});

                 			// Show new comment
                 			$( "#comment-wraper").prepend(data);
                 			$( "#comment-wraper div:first-child").effect("highlight", { color: "#ffc68c" }, 1200);

							// Clear form comment input
							clearFormComment();

							// Increase show number comment
							$('#count-comment').text(parseInt($('#count-comment').text()) + 1);

							// Enable button
                 			$(parent_id + ' .form-actions input[type="button"]').show();
                        }
                    });

                } //success
            });//ajax
            return false;
	});

    $('.reply').live("click", function(){
	    var id = this.id;
        $.ajax({
                type: "GET",
                url: "<?php echo Yii::app()->createUrl('site/loadFormReply', array('post_id' => $data->id))?>",
                success: function(data) {
					// Clear form insert comment
                	resetFormComment();

                 	// Show form reply
                	$("#comment-reply-" + id).show();
                	$("#comment-reply-" + id).html(data);

                	// Focus to position form reply show
                    offset = $("#comment-reply-" + id).offset();
                	$("html,body").animate({
                	    scrollTop: offset.top,
                	    scrollLeft: offset.left
                	});
                }
            });
            return false;
	    });
});
</script>